<?php
namespace  app\project\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Agentmanager extends Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('active','代理商管理');
            $this->assign('fun',$fun);
        }else{
            $this->redirect('index/login/index');
        }
    }
    public  function agent_list(){
        $this->assign('title','代理商列表');
        return $this->fetch();
    }
    public  function get_agent_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['a.agent_name']=['like','%'.$_GET['search'].'%'];
        }
        $state=array(0=>'待审核',1=>'已通过',2=>'未通过');
        $total=Db::name('agent_user')->alias('a')->where($where)->count();
        $agent= Db::name('agent_user')->alias('a')->field('a.*,b.id as user_id')->join('user b','a.name=b.username')->where($where)->limit($_GET['offset'],$_GET['limit'])->select();
        foreach ($agent as &$u){
            if( $u['audit_state']==0){
                $u['operation']='<a href="'.url('audit_agent').'?id='.$u['id'].'" class="btn btn-info">审核信息</a>';
            }else if( $u['audit_state']==1){
                $u['operation']='<a onclick="cancel_aduit('.$u['id'].')" class="btn btn-danger">取消审核</a>|<a onclick="recharge('.$u['user_id'].')" class="btn btn-warning">充值</a>';
            }else{
                $u['operation']='<a href="'.url('audit_agent').'?id='.$u['id'].'" class="btn btn-warning">重新审核</a>';
            }
            $u['audit_state']=$state[$u['audit_state']];

        }
        return array('rows'=>$agent,'total'=>$total);
    }
    public   function  audit_agent(){
        if(request()->isPost()){
            $data=input('post.');
            $info =Db::name('agent_user')->where('id',$data['id'])->update($data);
            if ($info){
                  return array('code'=>1,'msg'=>'审核完成','url'=>url('agent_list'));
            }else{
                return array('code'=>0,'msg'=>'审核失败');
            }
        }
            $agent=Db::name('agent_user')->where('id',$_GET['id'])->find();
            $this->assign('agent',$agent);
            $this->assign('title','审核代理商信息');
            return $this->fetch();
    }
    public   function  cancel_aduit(){
           $info =Db::name('agent_user')->where('id',$_GET['id'])->update(array('audit_state'=>0,'audit_note'=>''));
            if ($info){
                return array('code'=>1,'msg'=>'取消成功');
            }else{
                return array('code'=>0,'msg'=>'取消失败');
            }
    }
    public function add_agent_user(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('user')->insertGetId($data);
            if($id){
                $this->assign('msg','添加成功');
            }else{
                $this->assign('msg','添加失败');
            }
        }
        $this->assign('title','添加代理商账号');
        return $this->fetch();
    }
    public function get_username(){
        $info =Db::name('user')->where('username',$_GET['username'])->find();
        if ($info){
            return array('code'=>0,'msg'=>'用户名已被使用');
        }else{
            return array('code'=>1,'msg'=>'用户名可以使用');
        }
    }
    //充值金额
    //1,获取当前金额
    public function  get_user_amount(){
        $user_id=$_GET['user_id'];
        $amout=Db::name('user_balance')->where('user_id',$user_id)->find();
        if(empty($amout)){
            $user=Db::name('user')->where('id',$user_id)->find();
            $am['username']=$user['username'];
            $am['user_id']=$user['id'];
            $id=Db::name('user_balance')->insertGetId($am);
            if($id){
                $amout=Db::name('user_balance')->where('user_id',$user_id)->find();
                return array('code'=>1,'user'=>session('user'),'amount'=>$amout);
            }else{
                return array('code'=>0,'amount'=>'');
            }
        }else{
            return array('code'=>1,'user'=>session('user'),'amount'=>$amout);
        }
    }
    //充值
    public  function  recharge(){
        $data=input('post.');
        if($data){
            Db::startTrans();
            try{
                $amount=Db::name('user_balance')->where('user_id',$data['user_id'])->find();
                $amount['cash_balance']=$amount['cash_balance']+$data['recharge_amount'];
                $row=Db::name('user_balance')->where('id',$amount['id'])->update($amount);
                $id=Db::name('recharge_record')->insertGetId($data);
                if($row && $id){
                    Db::commit();
                    return array('code' =>1,'msg'=>'充值成功');
                }else{
                    Db::rollback();
                    return array('code' =>0,'msg'=>'充值失败');
                }
            } catch (\Exception $e) {
                Db::rollback();
                return array('code' =>-1,'msg'=>'充值失败');
            }

        }else{
            return array('code' =>-2,'msg'=>'丢失参数');
        }
    }


}